$(document).ready(function() {
    $("#date").datepicker({
        minDate: new Date(),
        maxDate: '+3m',
        changeMonth: true,
        changeYear: true,
        dateFormat: "yy-mm-dd",
        hideIfNoPrevNext: true,
        showButtonPanel: true
    });
});

function searchTicket() {
    var reference = $('#referenceno').val();
    if (reference != '') {
        var docHeight = $(window).height();
        $('#overlay').fadeIn('fast').height(docHeight);
        $('#referenceno').removeClass('error');
        $('#referenceErr').hide();
        var redirect = 'viewticket.php?mode=view&referenceno=' + reference;
        $('#overlay').fadeOut('fast');
        window.location.replace(redirect);
    } else {
        $('#referenceno').addClass('error');
        $('#referenceErr').show().html('Required field');
    }
}

function amendTicket(referenceno, route, origin, destin) {
    var docHeight = $(window).height();
    $('#overlay').fadeIn('fast').height(docHeight);
    $.post('action.php', {
        action: 'amendTicket', referenceno: referenceno, route: route, origin: origin, destin: destin
    }, function() {
        $('#overlay').fadeOut('fast');
        window.location.href = 'viewticket.php?mode=amend';
    });
}

function cancelTicket(referenceno) {
    var docHeight = $(window).height();
    $('#overlay').fadeIn('fast').height(docHeight);
    $.post('action.php', {
        action: 'cancelTicket', referenceno: referenceno
    }, function() {
        $('#overlay').fadeOut('fast');
        window.location.href = 'viewticket.php?mode=cancel';
    });
}

function confirmCancellation(reference, refund) {
    var docHeight = $(window).height();
    $('#overlay').fadeIn('fast').height(docHeight);
    $.post('action.php', {
        action: 'confirmCancellation', reference: reference, refund: refund
    }, function(data) {
        $('#overlay').fadeOut('fast');
        window.location.replace('success.php?refund=' + data.refund + '&referenceno=' + data.referenceno + '&balance=' + data.balance);
    });
}

function confirmAmendment() {
    if (confirm('Amend ticket? Note that you can only amend each ticket for one time.')) {
        var docHeight = $(window).height();
        $('#overlay').fadeIn('fast').height(docHeight);
        $.post('action.php', {
            action: 'confirmAmendment',
            reference: $('#reference').val(),
            seatid: $('#seatid').val()
        }, function(data) {
            $('#overlay').fadeOut('fast');
            window.location.replace('success.php?amend=' + data.amend + '&referenceno=' + data.referenceno + '&qrcode=' +data.qrcode);
        });
    }
}

function obtainSchedule() {
    var route = $('#route').val();
    var origin = $('#origin').val();
    var destin = $('#destin').val();
    var date = $('#date').val();
    if ((route != 0) && (origin != 0) && (destin != 0) && (date != '')) {
        var docHeight = $(window).height();
        $('#overlay').fadeIn('fast').height(docHeight);
        $.ajax({
            type: 'POST',
            url: 'action.php',
            data: {action: 'obtainSchedule', route: route, origin: origin, destin: destin, date: date},
            success: function(result) {
                if (!result.error) {
                    $('#schedule').html(result.message);
                    $(document).foundation('forms');
                    $('#overlay').fadeOut('fast');
                } else {
                    $('#schedule').html('');
                    $('#info').fadeIn('slow').addClass('alert').removeClass('success').html(result.message).delay(4000).fadeOut('slow');
                    $('#overlay').fadeOut('fast');
                }
            }
        });
    } else {
        $('#info').fadeIn('slow').addClass('alert').removeClass('success').html('Please make selection first').delay(4000).fadeOut('slow');
    }

}

function proceedBooking() {
    var index = $('#schedule input[name=index]:checked').val();
    var docHeight = $(window).height();
    $('#overlay').fadeIn('fast').height(docHeight);
    $.ajax({
        type: 'POST',
        url: 'action.php',
        data: {
            action: 'proceedAmend',
            shid: $('#shid' + index).val(),
            schedule: $('#schedule' + index).val(),
            departure: $('#departure' + index).val(),
            arrival: $('#arrival' + index).val(),
            origin: $('#origin' + index).val(),
            destin: $('#destin' + index).val(),
            route: $('#route' + index).val(),
            date: $('#date').val(),
            fare: $('#fare' + index).val(),
            emptyseat: $('#seat' + index).val()
        },
        success: function(data) {
            $('#selectseat').html(data);
            $('#overlay').fadeOut('fast');
        }
    });
}

function showModal(element) {
    $('#coachmap').html('');
    $('#myModal').foundation('reveal', 'open');
    var id = element.id;
    var docHeight = $(window).height();
    $('#overlay').fadeIn('fast').height(docHeight);
    $.ajax({
        type: 'POST',
        url: 'action.php',
        data: {action: 'getCoach'},
        success: function(result) {
            $('#coach').empty().append(new Option('Select Coach', 0));
            for (var i = 0; i < result.length; i++) {
                $('#coach').append(new Option(result[i], result[i]));
            }
            $(document).foundation('forms');
            $('#ticketnumber').val(id);
            $('#overlay').fadeOut('fast');
        }
    });
}

function getCoachmap() {
    if ($('#coach').val() != 0) {
        var docHeight = $(document).height();
        $('#overlay').fadeIn('fast').height(docHeight);
        $.ajax({
            type: 'POST',
            url: 'action.php',
            data: $('#seatform').serialize(),
            success: function(result) {
                $('#coachmap').html(result);
                $('#overlay').fadeOut('fast');
            }
        });
    }
}

function getSeatid(element, seatno, coach) {
    var id = $(element).attr('id');
    var ticketnumber = $('#ticketnumber').val();
    var seatid = 'seatid' + ticketnumber.substring(6);
    var docHeight = $(window).height();
    $('#overlay').fadeIn('fast').height(docHeight);
    var currentSeat = $('#' + seatid).val();
    if (currentSeat != '') {
        if (id != currentSeat) {
            $.post('action.php', {
                action: 'unlockSeat',
                id: currentSeat}
            );
        }
    }
    $('#' + seatid).val(id);
    $.post('action.php', {
        action: 'lockSeat',
        id: id
    }, function(data) {
        if (!data.error) {
            $('#' + ticketnumber).val(coach + ' - ' + seatno);
            $('#myModal').foundation('reveal', 'close');
        } else {
            alert(data.message);
        }
        $('#overlay').fadeOut('fast');
    });
}